#!/bin/tcsh -ef
####
#
#############################################################################
#                                                                           #
# Title: Refine Defocus (Not Recommended)                                   #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 12/03/2007                                             #
# Last Modification: 29/01/2015                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 77
#
# MANUAL: This script calls the MRC program ctfsearch to refine the defocus based on comparisson with the merged dataset.
#
# MANUAL: Be aware that this program may make things worse, if the data are noisy. Use with care, and first try to run it with the "Overwrite data" button set to "no". If the output appears to make sense, you can re-run it with "Overwrite data" set to "yes". 
#
# RESET: overwrite = "y"
#
# DISPLAY: lattice
# DISPLAY: realang
# DISPLAY: ALAT
# DISPLAY: TLTAXIS
# DISPLAY: TLTANG
# DISPLAY: TLTAXA
# DISPLAY: TAXA
# DISPLAY: TANGL
# DISPLAY: defocus
# DISPLAY: defocusbackup
# DISPLAY: SYM
# DISPLAY: phaori
# DISPLAY: revhk
# DISPLAY: revhnd
# DISPLAY: sgnxch
# DISPLAY: rot180
# DISPLAY: rot90
# DISPLAY: ctfrev
# DISPLAY: IQMAX
# DISPLAY: comment
# DISPLAY: ctfsearch_df_step
# DISPLAY: ctfsearch_df_range
# DISPLAY: ctfsearch_ncyc
# DISPLAY: ctfsearch_iphase
# DISPLAY: phacon
# DISPLAY: SYN_Unbending
# DISPLAY: refine_def_reference
# DISPLAY: refine_def_RESMAX
# DISPLAY: defocus_direction
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
set imagename = ""
set nonmaskimagename = ""
set imagesidelength = ""
set imagenumber = ""
set realcell = ""
set imagesidelength = ""
set magnification = ""
set stepdigitizer = ""
set tempkeep = ""
set revhk = ""
set rot180 = ""
set rot90 = ""
set revhnd = ""
set sgnxch = ""
set ctfrev = ""
set realang = ""
set RESMIN = ""
set RESMAX = ""
set ALAT = "" 
set lattice = ""
set IQMAX = ""
set defocus = ""
set defocusbackup = ""
set TLTAXIS = ""
set TLTANG = ""
set TLTAXA = ""
set TAXA = ""
set TANGL = ""
set SYM = ""
set phaori = ""
set CS = ""
set KV = ""
set RMAG = ""
set LCOLOR = ""
set ctfsearch_df_step = ""
set ctfsearch_df_range = ""
set ctfsearch_ncyc = ""
set ctfsearch_iphase = ""
set phacon = ""
set SYN_Unbending = ""
set refine_def_reference = ""
set refine_def_RESMAX = ""
set defocus_direction = ""
set overwrite = ""
#
#$end_vars
#
set scriptname = 2dx_refinedefocus
#
\rm -f LOGS/${scriptname}.results
#
set ccp4_setup = 'y'
source ${proc_2dx}/initialize
#
#############################################################################
${proc_2dx}/linblock "${scriptname}" 
#############################################################################  
#
echo bin_2dx = ${bin_2dx}
echo proc_2dx = ${proc_2dx}
#
echo SYM = ${SYM}
source ${proc_2dx}/2dx_sym2spcgrp_sub.com
echo spcgrp = ${spcgrp}
echo " "
#
#
source ${proc_2dx}/2dx_makedirs
#
echo "<<@progress: 1>>"
#
if ( ${defocus_direction}x == "1x" ) then
  set defocusbackup = ${defocus}
  echo "set defocusbackup = ${defocusbackup}" >> LOGS/${scriptname}.results
  echo "set defocus_direction = 0" >> LOGS/${scriptname}.results
  ${proc_2dx}/linblock "Backing up defocus values: ${defocus}"
  echo "<<@progress: 100>>"
  exit 
endif
#
if ( ${defocus_direction}x == "2x" ) then
  set defocus = ${defocusbackup}
  echo "set defocus = ${defocus}" >> LOGS/${scriptname}.results
  echo "set defocus_direction = 0" >> LOGS/${scriptname}.results
  ${proc_2dx}/linblock "Restoring defocus values: ${defocus}"
  echo "<<@progress: 100>>"
  exit 
endif
#
if ( ${rot180} == 'y' ) then
  set rot180val = '1'
else
  set rot180val = '0'
endif
#
if ( ${rot90} == 'y' ) then
  set rot90val = '1'
else
  set rot90val = '0'
endif
#
if ( ${revhk} == 'y' ) then
  set revhkval = '1'
else
  set revhkval = '0'
endif
#
if ( ${revhnd} == 'y' ) then
  set revhndval = '1'
else
  set revhndval = '0'
endif
#
if ( ${ctfrev} == 'y' ) then
  set ctfrevval = '1'
else
  set ctfrevval = '0'
endif
#
echo sgnxch = ${sgnxch}
set phaori_local = ${phaori}
#
if ( ${sgnxch} == "y" ) then
  set sgnxchval = 1
  set phaorix = `echo ${phaori_local} | cut -d\, -f1 `
  set phaoriy = `echo ${phaori_local} | cut -d\, -f2 | awk '{ s = -$1 } END { print s }'`
  set phaori_local = `echo ${phaorix},${phaoriy}`
else
  set sgnxchval = 0
endif
#
\rm -f LOGS/${imagename}.ctfs.wilson.log
\rm -f ctfsearch.dat
\rm -f WILSONCTF
\rm -f SUMMARY
\rm -f TMP345875.tmp
\rm -f TMP345876.tmp
#
if ( ${refine_def_reference} == "0" ) then
  ${proc_2dx}/linblock "Doing nothing."
  exit
endif
#
if ( ${refine_def_reference} == "1" ) then
  set iref=0
  set averagedat = ../../merge/APH/avrg2D.hk
  set averagedat = ../../merge/APH/sym2Dref.hk
  if ( ! -e ${averagedat} ) then
    set averagedat = ../merge/APH/avrg2D.hk
    set averagedat = ../merge/APH/sym2Dref.hk
    if ( ! -e ${averagedat} ) then
      ${proc_2dx}/protest "ERROR: no suitable reference file found."
    endif
  endif
  ${proc_2dx}/linblock "Using 2D reference APH file ${averagedat}"
endif
#
if ( ${refine_def_reference} == "2" ) then
  set iref=2
  set mergedat = ../../merge/merge2Dref_MRClefthanded.mtz
  if ( ! -e ${mergedat} ) then
    set mergedat = ../merge/merge2Dref_MRClefthanded.mtz
    if ( ! -e ${mergedat} ) then
      ${proc_2dx}/protest "ERROR: no suitable reference file found."
    endif
  endif
  ${proc_2dx}/linblock "Using 2D reference MTZ file ${mergedat}"
endif
#
if ( ${refine_def_reference} == "3" ) then
  set iref=0
  set averagedat = ../../merge/APH/avrg3D.hkl
  if ( ! -e ${averagedat} ) then
    set averagedat = ../merge/APH/avrg3D.hkl
    if ( ! -e ${averagedat} ) then
      ${proc_2dx}/protest "ERROR: no suitable reference file found."
    endif
  endif
  ${proc_2dx}/linblock "Using 3D reference APH file ${averagedat}"
endif
#
if ( ${refine_def_reference} == "4" ) then
  set iref=2
  set mergedat = ../../merge/merge3Dref_MRClefthanded.mtz
  if ( ! -e ${mergedat} ) then
    set mergedat = ../merge/merge3Dref_MRClefthanded.mtz
    if ( ! -e ${mergedat} ) then
      ${proc_2dx}/protest "ERROR: no suitable reference file found."
    endif
  endif
  ${proc_2dx}/linblock "Using 3D reference MTZ file ${mergedat}"
endif
#
if ( ${iref} == "0" ) then
  ${proc_2dx}/lin "Using ctfsearch with iref=${iref}"
  echo "# IMAGE-IMPORTANT: ${averagedat} <Reference APH file>" >> LOGS/${scriptname}.results
else
  ${proc_2dx}/lin "Using ctfsearch with iref=${iref}"
  echo "# IMAGE-IMPORTANT: ${mergedat} <Reference MTZ file>" >> LOGS/${scriptname}.results
endif
#
echo "<<@progress: 10>>"
#
echo "# IMAGE-IMPORTANT: ${imagename}.mrc <Image>" >> LOGS/${scriptname}.results
echo "# IMAGE-IMPORTANT: "${nonmaskimagename}.mrc "<Raw Image>" >> LOGS/${scriptname}.results
echo "# IMAGE-IMPORTANT: FFTIR/${nonmaskimagename}_red.mrc <Downsampled Image>" >> LOGS/${scriptname}.results
echo "# IMAGE-IMPORTANT: FFTIR/${nonmaskimagename}_fft.mrc <FFT of Image>" >> LOGS/${scriptname}.results
echo "# IMAGE-IMPORTANT: FFTIR/${nonmaskimagename}_red_fft.mrc <FFT of Downsampled Image>" >> LOGS/${scriptname}.results
#
set APH_infile = image_ctfcor_fou_unbent.aph
#
if ( ! -e APH/${APH_infile} ) then
  ${proc_2dx}/protest "ERROR: APH/${APH_infile} does not exist."
endif
echo "# IMAGE-IMPORTANT: APH/${APH_infile} <APH: Image data for refinement>" >> LOGS/${scriptname}.results
#
setenv INIMAGE APH/${APH_infile}
#
set IAQP2 = 0
set fittitle = "ctfsearch2 run"
#
set local_max = 4
set IQMAX_local = `echo ${IQMAX} ${local_max} | awk ' { if ( $1 > $2 ) { s = $2 } else { s = $1 } } END { print s }'`
#
######################################################################################
if ( $iref == 2 ) then
  ######################################################################################
  #
  setenv HKLIN ${mergedat}
  if ( ! -e ${mergedat} ) then
    ${proc_2dx}/protest "ERROR: Reference file not found: ${mergedat}"
  endif
  #
  set spcgrp_local = ${spcgrp}
  # set spcgrp_local = 1
  #
  #
  echo ": now comes ctfsearch2 with:"
  echo ":${bin_2dx}/2dx_ctfsearch2.exe"
  echo ": F F ${IQMAX_local}                                         ! LIST LISTW IQMAX"
  echo ":${lattice} ${imagesidelength} ${stepdigitizer} ${magnification}       ! AX AY BX BY ISIZEX DSTEP XMAG"
  echo ":${defocus} ${CS} ${KV} ${phacon}                           ! DFMID1 DFMID2 ANGAST CS KV PHACON"
  echo ":${fittitle}                                     ! TITLE FOR PLOT OF FINAL BEST FIT."
  echo ":${phaori_local} ${TAXA} ${TANGL} ${revhkval} ${rot180val} ${sgnxchval} ${rot90val} ${ctfrevval} ${IAQP2}        ! ORIGH ORIGK TAXA TANGL REVHK ROT180 SGNXCH ROT90 ICTFREV IAQP2"
  echo ":${RESMIN} ${refine_def_RESMAX}                             ! RESMIN RESMAX"
  echo ":${spcgrp_local} ${iref} ${ctfsearch_ncyc} ${ctfsearch_iphase} ${ctfsearch_df_step} ${ctfsearch_df_range} ! ISPGRP,IREF,NCYC,IPHASE,ctfsearch_df_step,ctfsearch_df_range"
  echo ":LABIN AMP=F PHASE=PHI FOM=FOM SIG=SIGF          ! IF IREF=2 MTZ CONTROL"
  echo " "
  #
  ${bin_2dx}/2dx_ctfsearch2.exe << eot
F F ${IQMAX_local}                                         ! LIST LISTW IQMAX
${lattice} ${imagesidelength} ${stepdigitizer} ${magnification}       ! AX AY BX BY ISIZEX DSTEP XMAG
${defocus} ${CS} ${KV} ${phacon}                         ! DFMID1 DFMID2 ANGAST CS KV PHACON
${fittitle}                                     ! TITLE FOR PLOT OF FINAL BEST FIT.
${phaori_local} ${TAXA} ${TANGL} ${revhkval} ${rot180val} ${sgnxchval} ${rot90val} ${ctfrevval} ${IAQP2}       ! ORIGH ORIGK TAXA TANGL REVHK ROT180 SGNXCH ROT90 ICTFREV IAQP2
${RESMIN} ${refine_def_RESMAX}                             ! RESMIN RESMAX
${spcgrp_local} ${iref} ${ctfsearch_ncyc} ${ctfsearch_iphase} ${ctfsearch_df_step} ${ctfsearch_df_range} ! ISPGRP,IREF,NCYC,IPHASE,ctfsearch_df_step,ctfsearch_df_range
LABIN AMP=F PHASE=PHI FOM=FOM SIG=SIGF          ! IF IREF=2 MTZ CONTROL
eot
  #
  ######################################################################################
else if ( $iref == 0 ) then
  ######################################################################################
  #
  if ( ! -e ${averagedat} ) then
    ${proc_2dx}/protest "ERROR: ${averagedat} not found."
  endif
  setenv INREF ${averagedat}
  #
  ${bin_2dx}/2dx_ctfsearch2.exe << eot
F F ${IQMAX_local}                                                 ! LIST LISTW IQMAX
${lattice} ${imagesidelength} ${stepdigitizer} ${magnification}               ! AX AY BX BY ISIZEX DSTEP XMAG
${defocus} ${CS} ${KV} ${phacon}                                 ! DFMID1 DFMID2 ANGAST CS KV PHACON
${fittitle}                                             ! TITLE FOR PLOT OF FINAL BEST FIT.
${phaori_local} ${TAXA} ${TANGL} ${revhkval} ${rot180val} ${sgnxchval} ${rot90val} ${ctfrevval} ${IAQP2}       ! ORIGH ORIGK TAXA TANGL REVHK ROT180 SGNXCH ROT90 ICTFREV IAQP2
${RESMIN} ${refine_def_RESMAX}                                     ! RESMIN RESMAX
${spcgrp} ${iref} ${ctfsearch_ncyc} ${ctfsearch_iphase} ${ctfsearch_df_step} ${ctfsearch_df_range}        ! ISPGRP,IREF,NCYC,IPHASE,ctfsearch_df_step,ctfsearch_df_range
                                                        ! FC SIGFC PHCAL FOM or LABIN AMP SIG PHS FOM
eot
  #
  ######################################################################################
else if ( $iref == '-1' ) then
  ######################################################################################
  #
  if ( ! -e ${averagedat} ) then
    ${proc_2dx}/protest "ERROR: ${averagedat} not found."
  endif
  setenv INREF ${averagedat}
  #
  ${bin_2dx}/2dx_ctfsearch2.exe << eot
F F ${IQMAX_local}                                         ! LIST LISTW IQMAX
${lattice} ${imagesidelength} ${stepdigitizer} ${magnification}       ! AX AY BX BY ISIZEX DSTEP XMAG
${defocus} ${CS} ${KV} ${phacon}                           ! DFMID1 DFMID2 ANGAST CS KV PHACON
${fittitle}                                     ! TITLE FOR PLOT OF FINAL BEST FIT.
${phaori_local} ${TAXA} ${TANGL} ${revhkval} ${rot180val} ${sgnxchval} ${rot90val} ${ctfrevval} ${IAQP2}        ! ORIGH ORIGK TAXA TANGL REVHK ROT180 SGNXCH ROT90 ICTFREV IAQP2
${RESMIN} ${refine_def_RESMAX}                             ! RESMIN RESMAX
${spcgrp} ${iref} ${ctfsearch_ncyc} ${ctfsearch_iphase} ${ctfsearch_df_step} ${ctfsearch_df_range} ! ISPGRP,IREF,NCYC,IPHASE,ctfsearch_df_step,ctfsearch_df_range
                                                ! FC SIGFC PHCAL FOM or LABIN AMP SIG PHS FOM
eot
  #
endif
######################################################################################
######################################################################################
#
if ( -e WILSONCTF ) then
  \mv -f WILSONCTF LOGS/${imagename}_ctfs_wilson.txt
  echo "# IMAGE-IMPORTANT: LOGS/${imagename}_ctfs_wilson.txt <LOG: Wilson>" >> LOGS/${scriptname}.results
endif
if ( -e SUMMARY ) then
  \mv -f SUMMARY LOGS/${imagename}_ctfs_summary.txt
  echo "# IMAGE-IMPORTANT: LOGS/${imagename}_ctfs_summary.txt <LOG: Summary>" >> LOGS/${scriptname}.results
endif
#
set olddef = ${defocus}
set tmpdef = "`cat TMP345876.tmp`"
set defocus = `echo ${tmpdef} | sed 's/ /,/g'`
#
\rm -f TMP345875.tmp
\rm -f TMP345876.tmp
#
${proc_2dx}/linblock "Old defocus is ${olddef}"
${proc_2dx}/linblock "New defocus is ${defocus}"
#
if ( ${overwrite} == "y" ) then
  echo "set defocus = ${defocus}" >> LOGS/${scriptname}.results
  ${proc_2dx}/linblock "Using new defocus of ${defocus}"
endif
#
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
#
echo "<<@progress: 100>>"
#
